package com.Akronmcp.hm;

import java.io.File;

import org.bukkit.command.CommandExecutor;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import com.Akronmcp.hta.obj.HManager;
import com.Akronmcp.hta.obj.HPar;
import com.Akronmcp.hta.obj.HopHolo;


public class HmM extends JavaPlugin{
	
	public static FileConfiguration conf;
	public static FileConfiguration lang;
	
	public static File df;
	
	public final static HManager hta = new HManager();

	public static JavaPlugin ins;
	
	@Override
	public void onEnable()
	{
		ins = this;
		
		df = getDataFolder();
   	 
	   	 if(!df.exists())
	   	 {
	   	   File filelangf=new File(df,"lang");
	   	   getDataFolder().mkdir(); 
	   	   filelangf.mkdir();
	   	   
		   	try
		   	 {
		   	  saveResource("config.yml", false);
		   	  saveResource("Mlist1.yml", false);
		   	  saveResource("Mlist2.yml", false);
		   	  saveResource("Mlist3.yml", false);
		   	  saveResource("Mlisten.yml", false);
		   	  saveResource("Mhoptext.yml", false);   	  
		   	  saveResource("假的教程(help).yml", false);
		   	  saveResource("真的教程(help).yml", false);
		   	  saveResource("lang/ZH_CN.yml", false);
		   	  //读取配置文件
		   	 }catch(Exception e)
		   	 {
		   		 e.printStackTrace();
		   	 }
		   	   
	   	 }
	   	 //创建插件数据目录and lang
		
	 
   	 
	    getServer().getPluginManager().registerEvents(new HmListener(), (Plugin)this);
  	    //注册监听器
	   	
	    CommandExecutor comer = new HmComer();
        getCommand("hm").setExecutor(comer);

        getCommand("hm").setTabCompleter((TabCompleter) comer);
     
  	    //注册指令执行器
	    
        try {
        	new HmLoader();
        	//载入配置
        	System.out.println(lang.getString("loadok"));

        }catch (Exception e) {
        	e.printStackTrace();
        	System.out.println(lang.getString("loadnotok"));
		}
	   	
	   	HmTimer.setInterval(20);
		HmTimer.goStart();
	   		   	
		getLogger().info("HM已启动");
	   	
		
//		Location lo = new Location(Bukkit.getWorld("world"), 0, 70, 3);
//		
//		ho = HologramsAPI.createHologram(this,lo);
//		
//		TextLine tl = ho.appendTextLine("233");
//		
//		ItemStack item = new ItemStack(Material.APPLE);
//		ho.appendItemLine(item);
//		
//		tl.setText("244{van}");
	
//		new BukkitRunnable() {
//			
//			int i = 0;
//			
//			@Override
//			public void run() {
//				
//				tl.setText(String.valueOf(i));
//				i++;
//				System.out.println(233+i);
//			}
//		}.runTaskTimerAsynchronously(this, 20, 20);
		
		
//		class HmPapi implements PlaceholderReplacer 
//		{
//			
//			String str;
//			
//			public HmPapi(String papistr)
//			{
//				str = papistr;
//			}
//			
//			@Override
//			public String update() {
//				// TODO Auto-generated method stub
//				return PlaceholderAPI.setPlaceholders(p, str);
//			}
//			
//		}
//		
//		HologramsAPI.registerPlaceholder(this,"van",10,new pl());
//		
	}
	
	
	public void onDisable()
	{
		 for(HopMess hm:HopMess.hmMap.values())
	   	 {//关闭所有公告线程
	   		hm.setEnable(false);
	   		for(HPar htx:hm.getHl())
				{
					if(htx instanceof HopHolo)
					{
						HopHolo holo = (HopHolo)htx;
						holo.delete();
					}
				}
	   	 }
	   	 //清空
		
		hta.clearAll();
	}
  

}
